package study.mq.rocketmq.producer.support.rocketmq;

import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * RocketMQAutoConfiguration
 * （1）send 线程
 * （2）callback 线程
 */
@Slf4j
@Configuration
public class RocketMQConfig {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @PostConstruct
    public void init() {
        ThreadFactory factory = ThreadUtil.createThreadFactoryBuilder()
                .setNamePrefix("callback-thread-")
                .build();
        ThreadPoolExecutor pool = ExecutorBuilder.create()
                .setCorePoolSize(10)
                .setThreadFactory(factory)
                .build();
        //发送线程
//        rocketMQTemplate.setAsyncSenderExecutor(pool);
        //回调线程
        DefaultMQProducer producer = rocketMQTemplate.getProducer();
        producer.setCallbackExecutor(pool);
    }
}
